package com.crm.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.dto.GetPoolDto;
import com.crm.dto.PoolDto;
import com.crm.entity.Business;
import com.crm.entity.Clue;
import com.crm.mapper.PoolMapper;
import com.crm.dto.BusinessDto;
import com.crm.entity.Pool;
import com.crm.mapper.UserMapper;
import com.crm.result.PageResultTow;
import com.crm.service.PoolService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@AllArgsConstructor
@Slf4j
public class PoolServiceImpl extends ServiceImpl<PoolMapper, Pool> implements PoolService {

    private final PoolMapper poolMapper;
    private final UserMapper userMapper;
    private final RedisTemplate redisTemplate;
//    @Override
//    public PageResultTow getPoolList(PoolDto poolDto) {
//        Page<Pool> poolPage = new Page<>(poolDto.getPageNum(), poolDto.getPageSize());
//        LambdaQueryWrapper<Pool> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(StringUtils.isNotEmpty(JSONUtil.toJsonStr(poolDto.getId())), Pool::getId, poolDto.getId());
//        wrapper.like(StringUtils.isNotEmpty(JSONUtil.toJsonStr(poolDto.getName())), Pool::getName, poolDto.getName());
//        wrapper.like(StringUtils.isNotEmpty(JSONUtil.toJsonStr(poolDto.getPhone())), Pool::getPhone, poolDto.getPhone());
//        wrapper.like(StringUtils.isNotEmpty(JSONUtil.toJsonStr(poolDto.getSubject())), Pool::getSubject, poolDto.getSubject());;
//        wrapper.between(StringUtils.isNotEmpty(JSONUtil.toJsonStr(poolDto.getBeginCreateTime())) && StringUtils.isNotEmpty(JSONUtil.toJsonStr(poolDto.getEndCreateTime())), Pool::getCreateTime, poolDto.getBeginCreateTime(), poolDto.getEndCreateTime());
//        poolMapper.selectPage(poolPage, wrapper);
//        List<Pool> records = poolPage.getRecords();
//        return new PageResultTow(200, "查询成功", null, poolPage.getTotal(),records);
//    }

    @Override
    public PageResultTow getPollListByPage(PoolDto poolDto) {
        if (poolDto.getParams().get("beginCreateTime") != null && !poolDto.getParams().get("beginCreateTime").isEmpty()) {
            LocalDate date = LocalDate.parse(poolDto.getParams().get("beginCreateTime"));
            poolDto.setBeginCreateTime(LocalDateTime.of(date, LocalTime.MIN));
        } else {
            poolDto.setBeginCreateTime(null);
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if (poolDto.getParams().get("beginCreateTime") != null && !poolDto.getParams().get("beginCreateTime").isEmpty()) {
            LocalDate date = LocalDate.parse(poolDto.getParams().get("beginCreateTime"), formatter);
            poolDto.setBeginCreateTime(LocalDateTime.of(date, LocalTime.MIN));
        } else {
            poolDto.setBeginCreateTime(null);
        }
        List<Long> longs = userMapper.selectByName(poolDto.getOwner());
        if (longs.size()==0){
            longs.add(-1L);
            poolDto.setUserids(longs);
        }else {
            poolDto.setUserids(longs);
        }
        PageHelper.startPage(poolDto.getPageNum(), poolDto.getPageSize());
        List<Business> businesses = poolMapper.selectByConditions(poolDto); // 直接传入 poolDto
        PageInfo<Business> pageInfo = new PageInfo<>(businesses);
        PageResultTow pageResultTow = new PageResultTow(200, "查询成功", null, pageInfo.getTotal(), pageInfo.getList());

        return pageResultTow;
    }

    @Override
    public void putGain(GetPoolDto getPoolDto) {
        poolMapper.putGain(getPoolDto.getIds());



        String key = "REPEAT_TIME";
        // 获取线索过期时间
        Integer repeatGetTime = (Integer) redisTemplate.opsForHash().get("task", "repeatGetTime");
        Integer repeatType = (Integer) redisTemplate.opsForHash().get("task", "repeatType");
        if (repeatGetTime != null) {
            // 设置线索，过期时间存线索到redis
            switch (repeatGetTime) {
                case 0:
                    redisTemplate.opsForValue().set(key, getPoolDto.getId(),24 * repeatGetTime, TimeUnit.HOURS);
                    break;
                case 1:
                    redisTemplate.opsForValue().set(key, getPoolDto.getId(), 7 * 24 * repeatGetTime, TimeUnit.HOURS);
                    break;
                case 2:
                    redisTemplate.opsForValue().set(key, getPoolDto.getId(),30 * 7 * 24 * repeatGetTime, TimeUnit.HOURS);
                    break;
                default:
                    log.info("未知捞取过期时限");
            }
        }

    }
}
